package com.un.ebs.sale.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.un.ebs.sale.domain.SaleDeliverNote;
import com.un.ebs.sale.domain.SaleDeliverNoteExt;
import com.un.ebs.sale.domain.SaleDeliverNoteItemExt;
import com.un.ebs.sale.dto.CanDeliverInventoryNoteItemDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * (SaleDeliverNote)表数据库访问层
 *
 * @author liubo
 * @since 2020-08-19 12:11:51
 */
public interface SaleDeliverNoteMapper extends BaseMapper<SaleDeliverNote> {

    @Select("SELECT ISNULL(MAX(RIGHT(code,4)),0) FROM dbo.tb_sale_deliver_note WHERE CONVERT(VARCHAR(6),create_time,112) = CONVERT(VARCHAR(6),GETDATE(),112)")
    int maxCode();

    @SqlParser(filter = true)
    @Select("select * from v_sale_deliver_note_ext where id = #{id}")
    SaleDeliverNoteExt selectViewById(@Param("id") String id);

    @SqlParser(filter = true)
    @Select("select * from v_sale_deliver_note_item_ext where note_id = #{id}")
    List<SaleDeliverNoteItemExt> selectItemViewById(@Param("id") String id);

    @Select("SELECT * FROM dbo.v_sale_deliver_note_item_ext WHERE inventory_note_id=#{inventoryNoteId} and sku_id=#{skuId}")
    SaleDeliverNoteItemExt selectItemByInventoryNoteIdAndSkuId(@Param("inventoryNoteId") String inventoryNoteId,@Param("skuId") String skuId);

    @Select("SELECT CAST(ISNULL(submit_status,0) as bit) FROM dbo.v_sale_deliver_note_item_ext WHERE inventory_note_id=#{inventoryNoteId} and sku_id=#{skuId}")
    Boolean queryDeliverStatusBySku(@Param("inventoryNoteId") String inventoryNoteId, @Param("skuId") String skuId);

    @Select("SELECT top 1 CAST(ISNULL(submit_status,0) as bit) FROM dbo.v_sale_deliver_note_item_ext WHERE inventory_note_id=#{inventoryNoteId}")
    Boolean queryDeliverStatus(@Param("inventoryNoteId") String inventoryNoteId);

    @Select("select isnull(sum(deliver_qty),0) from dbo.v_sale_deliver_note_item_ext where inventory_note_id=#{inventoryNoteId} and sku_id=#{skuId}")
    Double queryDeliverQty(@Param("inventoryNoteId") String inventoryNoteId, @Param("skuId") String skuId);

    @Select("SELECT * FROM dbo.fun_get_can_deliver_inventory_note_item(#{formalContractCode})")
    List<CanDeliverInventoryNoteItemDto> queryCanDeliverInventoryNoteItem(@Param("formalContractCode") String formalContractCode);

    @Select("SELECT CASE WHEN COUNT(*)>0 THEN 0 ELSE 1 end FROM dbo.v_inventory_note_item_ext WHERE note_id IN (SELECT DISTINCT inventory_note_id FROM dbo.tb_sale_deliver_note_item WHERE note_id=#{saleDeliverNoteId}) AND audit_status=0")
    boolean queryInventoryNoteAuditStatus(@Param("saleDeliverNoteId") String saleDeliverNoteId);

    @Select("SELECT COUNT(id) FROM dbo.tb_inventory_note WHERE back_parent_id IS NOT NULL AND id IN (SELECT inventory_note_id FROM dbo.tb_sale_deliver_note_item WHERE note_id=#{id})")
    int selectCountByIsBack(@Param("id") String id);

}